import java.util.Arrays;

public class Leet_LCP_28 {
    public static void main(String[] args) {
        System.out.println(purchasePlans(new int[]{3,1,2},3));
    }
    static public int purchasePlans(int[] nums, int target) {
        int num = 0;
        Arrays.sort(nums);
        int l = 0,r = nums.length-1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[0]+nums[i]>target){
                r = i-1;
                break;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            while (true){
                if (i==r)
                    break;
                if (nums[i]+nums[r]<=target){
                    num+=r-i;
                    break;
                }
                r--;
            }
            if (i==r)
                break;
        }
        return num;
    }
}